<?php


namespace app\api\controller;

use app\api\model\Logistics;
use app\Server\Redis;

class Express
{
    //快递物流
    public function ExpressAll(){

        //接受物流订单
        $LogisticsNo=input('not');
    //'75602025971867'
        if(empty($LogisticsNo)){
            return  json(['code'=>400,',msg'=>'参数不能为空','not'=>null]);
        }
        //'zhongtong'
        //接受物流公司名称
        $LogisticsType=input('type');

        if(empty($LogisticsType)){
            return  json(['code'=>400,',msg'=>'参数不能为空','type'=>null]);
        }
        //获取物流地址获取
        $LogisticsRest=json_decode(Goculr($LogisticsNo,$LogisticsType),true);

        //存入redis
        $RedisSet=Redis::setString($LogisticsNo,json_encode($LogisticsRest));

        //判断redis是否为空
        if(empty($RedisSet)){
            //存入redis
            $RedisSet=Redis::setString($LogisticsNo,json_encode($LogisticsRest));
            if($RedisSet){
                $Redisdata= json_decode(Redis::getString($LogisticsNo),true);
                return  json(['code'=>200,'msg'=>'查询成功','data'=>$Redisdata]);
            }

        }else{
            $Redisdata= json_decode(Redis::getString($LogisticsNo),true);
            return  json(['code'=>200,'msg'=>'查询成功','data'=>$Redisdata]);
        }



    }

    //定时任务 过半个小时更新一次
    static  public function work()
     {

         //获取所有未完成的物流
         $logisticsData = Logistics::where('status', 0)->select()->toArray();
         foreach ($logisticsData as $k => $v) {
             list($no, $type) = [$v['tracking'], $v['type']];
             $data =json_decode(Goculr($no, $type),true);

             if ($data['no'] == $no) {
                 Redis::setString($no, json_encode($data));
                 echo '成功';
             } else {
                 echo '存储错误，请查看redis';
             }
         }
     }

}
